/**
 * Created by nico on 2016/2/26.
 */
app.factory('BaseFactory',['$q','$ae',function($q,$ae){

    return {
        PAGE_SIZE:2,
        //显示任务首页,分别显示已领件、在派送、退换货、已取消、已完成、预约
        taskHome:function(uid){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.taskHome');
            query.invoke({uid:uid}).then(function(data){
                console.log(data);
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        taskWaitTake:function(uid){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.taskWaitTake');
            query.invoke({uid:uid}).then(function(data){
                console.log(data);
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },

        //获取所有在派送任务数据
        inExpress:function(uid,page,page_size){
            var q = $q.defer();
            var query = new $ae.Query('lg_mission');
            query.condition(" send_uid= " + uid) ;
            query.and(" status in (7000,8000)");
            query.and(" type not in (11,12)");
            query.page(page,page_size);
            query.findAndCount().then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },

        taskDetail:function(uid,code){
            var q = $q.defer();
            var query = new $ae.Query('lg_mission');
            query.condition(" send_uid='"+uid+"' and code= '" + code+"'") ;
            query.first().then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },

        //退货首页展示
        backGoods:function(uid,page,page_size){
            var q = $q.defer();
            var query = new $ae.Query('lg_mission');
            query.condition(" send_uid= " + uid) ;
            query.and(" type = "+11);
            query.and(" status in (2500,3000,4000,7000,8000) ");
            query.page(page,page_size)
            query.findAndCount().then(function(data){
                q.resolve(data);
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        //换货页面exchangeGoods
        exchangeGoods:function(uid,page,page_size){
            var q = $q.defer();
            var query = new $ae.Query('lg_mission');
            query.condition(" send_uid= " + uid) ;
            query.and(" type = "+12);
            query.and(" status in (2500,3000,4000,7000,8000) ");
            query.page(page,page_size)
            query.findAndCount().then(function(data){
                q.resolve(data);
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        //换货页面exchangeGoods
        hasFinished:function(uid){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.hasFinishedList');
            query.invoke({uid:uid}).then(function(data){
                //console.log(data);
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        //根据订单号搜索
        searchTask:function(uid,code,page,page_size){
            var q = $q.defer();
            var query = new $ae.Query('lg_mission');
            query.condition(" send_uid= " + uid) ;
            query.and(" is_account=0 and (code like '%"+code+"%' or src_code like '%"+code+"%')");
            query.page(page,page_size);
            query.findAndCount().then(function(data){
                q.resolve(data);
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        //预约派送时间更新数据库
        bookTime:function(uid,code,book_time){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.updateMissionAndFlow');
            var args = {uid:uid,code:code,status:6900,action:'BOOK',content:'/',book_time:book_time}
            query.invoke(args).then(function(data){
                console.log(data);
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },
        //预约
        booktimeList:function(uid,page,page_size){
            var q = $q.defer();
            var query = new $ae.Query('lg_mission');
            //query.condition(" send_uid= " + uid) ;
            query.condition(" send_uid = "+uid);
            query.and(" book_time is not null and status=6900 ");
            query.page(page,page_size)
            query.findAndCount().then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject({code:-1,msg:'未找到您的任务信息，请退出重试',err:err})
            })
            return q.promise ;
        },
        //重新开始任务
        restartExpress:function(uid,code){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.updateMissionAndFlow');
            var args = {uid:uid,code:code,status:7000,action:'RESTART',content:'/'}
            query.invoke(args).then(function(data){
                console.log(data);
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },
        //完成派送时更新数据库
        finishExpress:function(uid,code){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.updateMissionAndFlow');
            var args = {uid:uid,code:code,status:9000,action:'FINISH',content:'/'}
            query.invoke(args).then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },
        //拒收
        rejectExpress:function(uid,code,dialog){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.updateMissionAndFlow');
            var args = {uid:uid,code:code,status:9004,action:'REJECT',content:dialog}
            query.invoke(args).then(function(data){
                console.log(data);
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },

        //立即交账
        accountNow:function(uid){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.accountNow');
            var args = {uid:uid}
            query.invoke(args).then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },
        //拒收列表
        rejectList:function(uid,page,page_size){
            var q = $q.defer();
            var query = new $ae.Query('lg_mission');
            query.condition(" send_uid= " + uid) ;
            query.and(" status = 9004");
            query.page(page,page_size);
            query.findAndCount().then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        //待领取任务列表
        waitList:function(uid){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.taskWaitTakeList');
            query.invoke({uid:uid}).then(function(data){
                console.log(data);
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        /**
         * 营销
         */
        //营销首页
        sellsHome:function(){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.marketTypeList');
            var args = {}
            query.invoke(args).then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        //获取商品列表
        sellProList:function(name,marketType){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.marketProList');
            var args = {marketType:marketType,name:name}
            console.log(args)
            query.invoke(args).then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        //添加购物车
        addToCart:function(uid,pid,psid,number,productType){
            var q = $q.defer();
            var query = new $ae.Query('lg_courier_cart');
            query.condition(" uid = '" + uid + "'") ;
            query.and(" pid = '"+pid+"'");
            query.and(" psid = '"+psid+"'");
            query.first().then(function(data){
                if(data.get('id')){
                    //找到了，则更新
                    data.set({number:data.get('number')+number});
                    data.save().then(function(new_data){
                        q.resolve({code:0});
                    }).catch(function(err){
                        q.reject({code:-1});
                    });
                }else{
                    //没找到，创建新的
                    var res = new $ae.Object('lg_courier_cart');
                    res.set({uid:uid+"",pid:pid+"",psid:psid+"",number:number,productType:productType});
                    res.create().then(function(new_data){
                        q.resolve({code:0});
                    }).catch(function(err){
                        q.reject({code:-1});
                    });
                }
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },
        //获取购物车数据
        getCourerCart:function(uid){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.getCourierCart');
            var args = {uid:uid}
            query.invoke(args).then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            })
            return q.promise ;
        },

        trashCartOne:function(uid,pid){
            var q = $q.defer();
            var query = new $ae.Query('lg_courier_cart');
            query.condition(" uid= '" + uid+"'") ;
            query.and(" pid= '" + pid + "'") ;
            query.first().then(function(data){
                if(data.get('id')){
                    //删除
                    var res = new $ae.Object('lg_courier_cart');
                    res.getById(data.get('id')).then(function(data){
                        if(data.objectId){
                            res.remove({id:data.objectId}).then(function(){
                                q.resolve({code:0});
                            }).catch(function(err){
                                console.log(err)
                                q.reject(err)
                            });
                        }
                    }).catch(function(err){
                        console.log(err)
                        q.reject(err)
                    });
                }else{
                    //不删除
                    q.resolve({code:-1})
                }
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },
        trashCartAll:function(uid){
            var q = $q.defer();
            var query = new $ae.Query('lg_courier_cart');
            query.condition(" uid= '" + uid+"'") ;
            query.clear().then(function(data){
                q.resolve(data);
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },
        //记录GPS位置
        sync:function(client,geo){
            var q = $q.defer();
            var o = new $ae.Object('lg_location_record');
            //当前用户的消息
            o.set('clientId',''+client);
            o.set('latitude',''+geo.latitude);
            o.set('longitude',''+geo.longitude);
            //全局的通知消息
            o.create().then(function(data){
                q.resolve(data);
            }).catch(function(err){
                q.reject(err);
            });
            return q.promise;
        },

        //营销下单
        createMarketOrder:function(arg){
            var q = $q.defer();
            var query = new $ae.Function('order');
            var args = {
                "action":"createOrder",
                "type":"8",
                "toName":encodeURI(arg.toName+""),
                "toPhone":encodeURI(arg.toPhone+""),
                "toStreet":encodeURI(arg.toStreet+""),
                "total":arg.total+"",
                "remark":encodeURI(arg.remark+""),
                "uid":arg.uid+""
            };
            query.invoke(args).then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        },
        updateCart:function(uid,cart){
            var q = $q.defer();
            var query = new $ae.Function('api.logistics.updateCourierCart');
            var args = {uid:uid,row:cart};
            query.invoke(args).then(function(data){
                q.resolve(data)
            }).catch(function(err){
                q.reject(err)
            });
            return q.promise ;
        }

    }
}]);